package com.chuangyang.fixboxmaster.ui.fragment.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.chuangyang.fixboxlib.widgets.PullDownRefreshListView;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.NewOrder;
import com.chuangyang.fixboxmaster.service.FixBoxApi;
import com.chuangyang.fixboxmaster.service.GsonRequest;
import com.chuangyang.fixboxmaster.ui.ModuleActivity;
import com.chuangyang.fixboxmaster.ui.adapter.HomeListAdapter;
import com.chuangyang.fixboxmaster.ui.fragment.BaseFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/**
 * 首页
 */

public class HomeFragment extends BaseFragment implements AdapterView.OnItemClickListener, PullDownRefreshListView.OnRefreshListener {
    private static final String TAG = HomeFragment.class.getSimpleName();
    private View mContentView;
    private HomeListAdapter mAdapter;
    private ArrayList<NewOrder> mOrderList = new ArrayList<>();
    private PullDownRefreshListView mListView;
    private String currentStatus;


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.fragment_home_new, container, false);
        mListView = (PullDownRefreshListView) mContentView.findViewById(R.id.listview);
        mAdapter = new HomeListAdapter(mOrderList);
        mListView.setAdapter(mAdapter);
        mListView.setOnRefreshListener(this);
        mListView.setOnItemClickListener(this);
        mListView.setOnItemClickListener(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setContentView(mContentView);
        loadData();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onRefresh() {
        loadData();
    }

    public void loadData() {
        mListView.setLoading();
        loadDataWithoutFootLoading();
    }


    public void loadDataWithoutFootLoading() {
        HashMap<String, String> param = new HashMap<>();
        param.put("pageIndex", String.valueOf(mOrderList.size()));
        param.put("status", currentStatus);
        addRequest(new GsonRequest<>(FixBoxApi.GET_ALL_MY_ORDER_31, param, NewOrder.NewOrderInfo.class, responseListener));
    }


    public void updateListView() {
        mOrderList.clear();
        mListView.setAdapter(mAdapter);
        loadDataWithoutFootLoading();
    }


    private Response.Listener<NewOrder.NewOrderInfo> responseListener = new Response.Listener<NewOrder.NewOrderInfo>() {
        @Override
        public void onResponse(NewOrder.NewOrderInfo response) {
            if (response.result.orderList == null||response.result.orderList.length==0) {
                setEmptyView();
            }else {
                mOrderList.addAll(Arrays.asList(response.result.orderList));
                mAdapter.notifyDataSetChanged();
                showResult();
                if (mOrderList.size() >= response.result.totalCount)
                    mListView.setLoadingEnd();
                else {
                    mListView.setLoadingIdle();
                }
            }
        }
    };

    public void setStatus(String status){
        this.currentStatus =status;
    }


    @Override
    public ListView getListView() {
        return mListView;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (mOrderList.get(position).appType.equals("1000")) {
            Intent intentAddress = new Intent(getActivity(), ModuleActivity.class);
            intentAddress.putExtra(ModuleActivity.TYPE, ModuleActivity.B2B_ORDER_DETAL_ACTIVITY);
            intentAddress.putExtra(ModuleActivity.ORDER_ID, String.valueOf(mOrderList.get(position).id));
            startActivity(intentAddress);
        } else {
            Intent intentAddress = new Intent(getActivity(), ModuleActivity.class);
            intentAddress.putExtra(ModuleActivity.TYPE, ModuleActivity.ORDER_DETAL_ACTIVITY);
            intentAddress.putExtra(ModuleActivity.ORDER_ID, String.valueOf(mOrderList.get(position ).id));
            intentAddress.putExtra(ModuleActivity.TRANSCATION_ID, String.valueOf(mOrderList.get(position).transactionId));
            startActivity(intentAddress);
        }
    }
}
